/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.organizeComment;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.offline.OfflineService;
import shop.services.front.offline.bean.Offline;
import shop.services.front.offlineAccount.OfflineAccountService;
import shop.services.front.offlineAccount.bean.OfflineAccount;
import shop.services.front.organizeComment.OrganizeCommentService;
import shop.services.front.organizeComment.bean.OrganizeComment;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**   
 * @类名称：OrganizeCommentController
 * @创建人：Ltz   
 * @创建时间：2017-08-07 下午14:59:52      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/front/organizeComment")
public class OrganizeCommentController extends FrontBaseController<OrganizeComment> {
    @Resource
    private OrganizeCommentService organizeCommentService;
    @Resource
    private OfflineAccountService offlineAccountService;
    @Resource
    private OfflineService offlineService;
    @Override
    public Services<OrganizeComment> getService() {
        return organizeCommentService;
    }

    /**
     *跳转到添加评价页面
     * @return
     */
    @RequestMapping("/toAddOrganizeComment")
    public String toAddOrganizeComment() {
        return "/front/organizeComment/addOrganizeComment";
    }

    /**
     * 跳转到评价详情页面
     * @return
     */
    @RequestMapping("/toOrganizeCommentDetail")
    public String toOrganizeCommentDetail() {
        return "/front/organizeComment/organizeCommentDetail";
    }

    /**
     * 添加评价（机构）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertOrganizeComment", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertOrganizeComment(HttpServletRequest request, @ModelAttribute("organizeComment") OrganizeComment organizeComment) throws Exception {
        jsonResult = new JSONResult();
        List<OrganizeComment> rsList=organizeCommentService.selectMyOrganizeComment(organizeComment);
        for(OrganizeComment rs : rsList){//判断是否重复评论机构（同一个活动）
            if(rs.getOfflineID().equals(organizeComment.getOfflineID())){
                jsonResult.setSuccess(false);
                jsonResult.setMessage("您已通过该活动进行机构评价，请勿重复评论！");
                return jsonResult;
            }
        }
        OfflineAccount offlineAccount=new OfflineAccount();
        offlineAccount.setAccountID(organizeComment.getAccountID());
        offlineAccount.setOfflineID(organizeComment.getOfflineID());
        offlineAccount=offlineAccountService.selectOne(offlineAccount);//查询参与记录
        organizeCommentService.insert(organizeComment);
        offlineAccount.setIsOrganizeComment("y");//更新评论状态
        offlineAccountService.update(offlineAccount);
        return jsonResult;
    }

    /**
     * 查询机构评价
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectOrganizeComment")
    @ResponseBody
    public JSONResult selectOrganizeComment(HttpServletRequest request, @ModelAttribute("organizeComment") OrganizeComment organizeComment) throws Exception {
        Offline offline =new Offline();
        offline.setId(organizeComment.getOfflineID());
        offline=offlineService.selectOne(offline);
        organizeComment.setOrganizeID(offline.getOrganizeID());
        List<OrganizeComment> rsList = organizeCommentService.selectOrganizeComment(organizeComment);
        jsonResult = new JSONResult();
        jsonResult.setData(rsList);
        return jsonResult;
    }

}
